//
//  NewHomeHelper2Cell.swift
//  d11
//
//  Created by peale_pro on 2019/6/27.
//  Copyright © 2019年 Tech_001. All rights reserved.
//

import UIKit
typealias HelperBtnClickBlock3 = (_ tag: Int) -> Void
typealias BonusClickBlock3 = (_ tag: Int) -> Void
class NewHomeHelper2Cell: UITableViewCell {
    
    var bonusClickBlock3: BonusClickBlock3?
    func callChangeBlock(_ block: @escaping BonusClickBlock3) {
        
        bonusClickBlock3 = block
    }
    var balanceBgImageView: UIImageView?
    var backwaterBtn : UIButton?
    var backwaterImgView: UIImageView?
    var backwaterLabel: UILabel?
    var backwaterMoneyLabel: UILabel?
    
    var dividendBtn : UIButton?
    var dividendImgView: UIImageView?
    var dividendLabel: UILabel?
    var dividendMoneyLabel: UILabel?
    var lineView :UIView?
    
    var helperBtnClickBlock3: HelperBtnClickBlock3?
    func callClickBlock(_ block: @escaping HelperBtnClickBlock3) {
        
        helperBtnClickBlock3 = block
    }
    var bgImageView : UIImageView?
    var iconImageView : UIImageView?
    var titleLabel : UILabel?
    var moreBtn :UIButton?
    var lineXView : UIView?
    
    var gridBtn1 : UIButton?
    var gridImgView1: UIImageView?
    var gridLabel1: UILabel?
    
    var gridBtn2 : UIButton?
    var gridImgView2: UIImageView?
    var gridLabel2: UILabel?
    
    var gridBtn3 : UIButton?
    var gridImgView3: UIImageView?
    var gridLabel3: UILabel?
    
    var gridBtn4 : UIButton?
    var gridImgView4: UIImageView?
    var gridLabel4: UILabel?
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    func setUpUI() -> Void {
        self.backgroundColor  = HexRGBAlpha(0xf4f7f9, 1)
        self.selectionStyle = UITableViewCell.SelectionStyle.none
        self.selectionStyle = UITableViewCell.SelectionStyle.none
        self.balanceBgImageView = UIImageView.init(image:
        imageNamed("icon_home_hlfs_bg")?
            .resizableImage(withCapInsets: UIEdgeInsets(top: 5, left: 15, bottom: 5, right: 15),
                            resizingMode: .stretch))
        self.balanceBgImageView?.isUserInteractionEnabled = true
        self.contentView.addSubview( self.balanceBgImageView!)
        self.balanceBgImageView?.snp.makeConstraints{(make) in
            make.width.equalTo(280-10)
            make.height.equalToSuperview()
            make.top.equalToSuperview()
            make.left.equalToSuperview().offset(10)
        }
        self.backwaterBtn = UIButton.init(type: .custom)
        self.backwaterBtn?.addTarget(self, action: #selector(self.actionToClick3(_:)), for: .touchUpInside)
        self.backwaterBtn?.tag = 1001
        self.balanceBgImageView?.addSubview(self.backwaterBtn!)
        self.backwaterBtn?.snp.makeConstraints{(make) in
            make.width.equalTo((280-20))
            make.height.equalTo((190-12)/2)
            make.top.equalToSuperview().offset(6)
            make.centerX.equalToSuperview()
        }
        
        self.backwaterImgView = UIImageView.init(image: imageNamed("img_default_60"))
        self.backwaterBtn?.addSubview(self.backwaterImgView!)
        self.backwaterImgView?.snp.makeConstraints{(make) in
            make.width.equalTo(65)
            make.height.equalTo(65)
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-3)
        }
        
        self.backwaterLabel =  UILabel.init()
        self.backwaterLabel?.font = UIFont.systemFont(ofSize: 14)
        backwaterLabel?.textColor = UIColor.black
        backwaterLabel?.textAlignment = .left
        backwaterLabel?.text = "返水到账"
        self.backwaterBtn?.addSubview(self.backwaterLabel!)
        self.backwaterLabel?.snp.makeConstraints{(make) in
            make.left.equalToSuperview().offset(10)
            make.height.equalTo(20)
            make.centerY.equalToSuperview().offset(-5)
            make.right.equalTo((self.backwaterImgView?.snp.left)!).offset(-5)
        }
        
        self.backwaterMoneyLabel =  UILabel.init()
        self.backwaterMoneyLabel?.font = UIFont.systemFont(ofSize: 14)
        backwaterMoneyLabel?.textColor = HexRGBAlpha(0xffc569,1)
        backwaterMoneyLabel?.textAlignment = .left
        backwaterMoneyLabel?.text = "-"
        self.backwaterBtn?.addSubview(self.backwaterMoneyLabel!)
        self.backwaterMoneyLabel?.snp.makeConstraints{(make) in
            make.left.equalToSuperview().offset(10)
             make.height.equalTo(20)
            make.top.equalTo((backwaterLabel?.snp.bottom)!)
            make.right.equalTo((self.backwaterImgView?.snp.left)!).offset(-5)
        }
        
        
        
        //65 icon_home_fs icon_home_hl
        
        self.lineView = UIView.init()
        self.lineView?.backgroundColor = HexRGBAlpha(0xffc569,1)
        self.balanceBgImageView?.addSubview(self.lineView!)
        self.lineView?.snp.makeConstraints{(make) in
            make.width.equalToSuperview().offset(-20)
            make.height.equalTo(1)
            //make.left.equalTo((self.backwaterBtn?.snp.right)!)
            make.top.equalTo((self.backwaterBtn?.snp.bottom)!)
            make.centerX.equalToSuperview()
        }
        
        self.dividendBtn = UIButton.init(type: .custom)
        self.dividendBtn?.addTarget(self, action: #selector(self.actionToClick(_:)), for: .touchUpInside)
        self.dividendBtn?.tag = 1002
        self.balanceBgImageView?.addSubview(self.dividendBtn!)
        self.dividendBtn?.snp.makeConstraints{(make) in
            make.width.equalTo(280-20)
            make.height.equalTo((190-12)/2)
            make.centerX.equalToSuperview()
            make.top.equalTo((self.lineView?.snp.bottom)!)
        }
        
        self.dividendImgView = UIImageView.init(image: imageNamed("img_default_60"))
        self.dividendBtn?.addSubview(self.dividendImgView!)
        self.dividendImgView?.snp.makeConstraints{(make) in
            make.width.equalTo(65)
            make.height.equalTo(65)
            make.centerY.equalToSuperview()
            make.right.equalToSuperview()
        }
        
        self.dividendLabel =  UILabel.init()
        self.dividendLabel?.font = UIFont.systemFont(ofSize: 14)
        dividendLabel?.textColor = UIColor.black
        dividendLabel?.textAlignment = .left
        dividendLabel?.text = "最新红利"
        self.dividendBtn?.addSubview(self.dividendLabel!)
        self.dividendLabel?.snp.makeConstraints{(make) in
            make.left.equalToSuperview().offset(15)
            make.height.equalTo(20)
            make.centerY.equalToSuperview().offset(-5)
            make.right.equalTo((self.dividendImgView?.snp.left)!).offset(-5)
        }
        
        self.dividendMoneyLabel =  UILabel.init()
        self.dividendMoneyLabel?.font = UIFont.systemFont(ofSize: 14)
        dividendMoneyLabel?.textColor = HexRGBAlpha(0xffc569,1)
        dividendMoneyLabel?.textAlignment = .left
        dividendMoneyLabel?.text = "-"
        self.dividendBtn?.addSubview(self.dividendMoneyLabel!)
        self.dividendMoneyLabel?.snp.makeConstraints{(make) in
            make.left.equalToSuperview().offset(15)
            make.height.equalTo(20)
            make.top.equalTo((dividendLabel?.snp.bottom)!)
            make.right.equalTo((self.dividendImgView?.snp.left)!).offset(-5)
        }
        
        self.bgImageView = UIImageView.init()
        self.bgImageView?.image =
        imageNamed("icon_home_top_bg")?
            .resizableImage(withCapInsets: UIEdgeInsets(top: 5, left: 15, bottom: 5, right: 15),
                            resizingMode: .stretch)
        self.bgImageView?.isUserInteractionEnabled = true
        self.contentView.addSubview(self.bgImageView!)
        self.bgImageView?.snp.makeConstraints{(make) in
            make.width.equalTo(kScreenWidth-280-10)
            make.height.equalTo(190)
            make.left.equalTo((self.balanceBgImageView?.snp.right)!)
            make.top.equalToSuperview()
        }
        
        self.iconImageView = UIImageView.init(image: UIImage.init(named: "icon_home_line"))
        self.bgImageView?.addSubview(self.iconImageView!)
        iconImageView?.snp.makeConstraints{(make) in
            make.left.equalToSuperview().offset(12)
            make.width.equalTo(2)
            make.height.equalTo(16.5)
            make.top.equalToSuperview().offset(17)
        }
        
        self.moreBtn = UIButton.init(type: .custom)
        self.moreBtn?.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        self.moreBtn?.setTitle("更多 >>", for: .normal)
        self.moreBtn?.setTitleColor(HexRGBAlpha(0xffc569, 1), for: .normal)
        self.moreBtn?.tag = 1000
        self.moreBtn?.addTarget(self, action: #selector(self.actionToClick(_:)), for: .touchUpInside)
        self.bgImageView?.addSubview(self.moreBtn!)
        moreBtn?.snp.makeConstraints{(make) in
            make.right.equalToSuperview().offset(-6)
            make.width.equalTo(65)
            make.height.equalTo(40)
            make.top.equalToSuperview().offset(5)
        }
        
        self.titleLabel =  UILabel.init()
        self.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        titleLabel?.textColor =  HexRGBAlpha(0x424242, 1)
        self.selectionStyle = UITableViewCell.SelectionStyle.none
        titleLabel?.textAlignment = .left
        titleLabel?.text = "万博助手"
        self.bgImageView?.addSubview(self.titleLabel!)
        self.titleLabel?.snp.makeConstraints{(make) in
            make.left.equalTo((self.iconImageView?.snp.left)!).offset(12)
            make.right.equalTo((self.moreBtn?.snp.right)!).offset(-4)
            make.height.equalTo(40)
            make.top.equalToSuperview().offset(5)
        }
        
        self.lineXView = UIView.init()
        self.lineXView?.backgroundColor = HexRGBAlpha(0xffe2b4, 1)
        self.bgImageView?.addSubview(self.lineXView!)
        lineXView?.snp.makeConstraints{(make) in
            make.left.equalTo((self.iconImageView?.snp.left)!)
            make.right.equalToSuperview().offset(-12)
            make.height.equalTo(1)
            make.top.equalTo((self.titleLabel?.snp.bottom)!)
        }
        self.gridBtn1 = UIButton.init(type: .custom)
        self.gridBtn1?.tag = 1001
        self.gridBtn1?.addTarget(self, action: #selector(self.actionToClick(_:)), for: .touchUpInside)
        self.bgImageView?.addSubview(self.gridBtn1!)
        self.gridBtn1?.snp.makeConstraints{(make) in
            make.width.equalTo((kScreenWidth-280-50)/4)
            make.left.equalToSuperview().offset(15)
            make.height.equalTo(70)
            make.top.equalTo((lineXView?.snp.bottom)!).offset(25)
        }
        
        self.gridImgView1 = UIImageView.init()
        self.gridBtn1?.addSubview(self.gridImgView1!)
        self.gridImgView1?.snp.makeConstraints{(make) in
            make.width.equalTo(40)
            make.height.equalTo(40)
            make.centerX.equalToSuperview()
            make.top.equalTo(2)
        }
        
        
        self.gridLabel1 = UILabel.init()
        self.gridLabel1?.font = UIFont.systemFont(ofSize: 14)
        gridLabel1?.textColor = HexRGBAlpha(0x747474, 1)
        gridLabel1?.textAlignment = .center
        self.gridBtn1?.addSubview(self.gridLabel1!)
        self.gridLabel1?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
            make.top.equalTo((self.gridImgView1?.snp.bottom)!).offset(12)
        }
        
        self.gridBtn2 = UIButton.init(type: .custom)
        self.gridBtn2?.setTitleColor(UIColor.black, for: .normal)
        self.gridBtn2?.tag = 1002
        self.gridBtn2?.addTarget(self, action: #selector(self.actionToClick(_:)), for: .touchUpInside)
        self.bgImageView?.addSubview(self.gridBtn2!)
        self.gridBtn2?.snp.makeConstraints{(make) in
            make.width.equalTo((kScreenWidth-280-50)/4)
            make.height.equalTo(70)
            make.left.equalTo((self.gridBtn1?.snp.right)!)
            make.top.equalTo((lineXView?.snp.bottom)!).offset(25)
        }
        
        self.gridImgView2 = UIImageView.init()
        self.gridBtn2?.addSubview(self.gridImgView2!)
        self.gridImgView2?.snp.makeConstraints{(make) in
            make.width.equalTo(40)
            make.height.equalTo(40)
            make.centerX.equalToSuperview()
            make.top.equalTo(2)
        }
        
        
        self.gridLabel2 = UILabel.init()
        self.gridLabel2?.font = UIFont.systemFont(ofSize: 14)
        gridLabel2?.textColor = HexRGBAlpha(0x747474, 1)
        gridLabel2?.textAlignment = .center
        self.gridBtn2?.addSubview(self.gridLabel2!)
        self.gridLabel2?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
            make.top.equalTo((self.gridImgView2?.snp.bottom)!).offset(12)
        }
        
        self.gridBtn3 = UIButton.init(type: .custom)
        self.gridBtn3?.setTitleColor(UIColor.black, for: .normal)
        self.gridBtn3?.tag = 1003
        self.gridBtn3?.addTarget(self, action: #selector(self.actionToClick(_:)), for: .touchUpInside)
        self.bgImageView?.addSubview(self.gridBtn3!)
        self.gridBtn3?.snp.makeConstraints{(make) in
            make.width.equalTo((kScreenWidth-280-50)/4)
            make.height.equalTo(70)
            make.left.equalTo((self.gridBtn2?.snp.right)!)
            make.top.equalTo((lineXView?.snp.bottom)!).offset(25)
        }
        self.gridImgView3 = UIImageView.init()
        self.gridBtn3?.addSubview(self.gridImgView3!)
        self.gridImgView3?.snp.makeConstraints{(make) in
            make.width.equalTo(40)
            make.height.equalTo(40)
            make.centerX.equalToSuperview()
            make.top.equalTo(2)
        }
        
        
        self.gridLabel3 = UILabel.init()
        self.gridLabel3?.font = UIFont.systemFont(ofSize: 14)
        gridLabel3?.textColor = HexRGBAlpha(0x747474, 1)
        gridLabel3?.textAlignment = .center
        self.gridBtn3?.addSubview(self.gridLabel3!)
        self.gridLabel3?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
            make.top.equalTo((self.gridImgView3?.snp.bottom)!).offset(12)
        }
        
        self.gridBtn4 = UIButton.init(type: .custom)
        self.gridBtn4?.setTitleColor(UIColor.black, for: .normal)
        self.gridBtn4?.tag = 1004
        self.gridBtn4?.addTarget(self, action: #selector(self.actionToClick(_:)), for: .touchUpInside)
        self.bgImageView?.addSubview(self.gridBtn4!)
        self.gridBtn4?.snp.makeConstraints{(make) in
            make.width.equalTo((kScreenWidth-280-50)/4)
            make.height.equalTo(70)
            make.left.equalTo((self.gridBtn3?.snp.right)!)
            make.top.equalTo((lineXView?.snp.bottom)!).offset(25)
        }
        self.gridImgView4 = UIImageView.init()
        self.gridBtn4?.addSubview(self.gridImgView4!)
        self.gridImgView4?.snp.makeConstraints{(make) in
            make.width.equalTo(40)
            make.height.equalTo(40)
            make.centerX.equalToSuperview()
            make.top.equalTo(2)
        }
        
        
        self.gridLabel4 = UILabel.init()
        self.gridLabel4?.font = UIFont.systemFont(ofSize: 14)
        gridLabel4?.textColor = HexRGBAlpha(0x747474, 1)
        gridLabel4?.textAlignment = .center
        self.gridBtn4?.addSubview(self.gridLabel4!)
        self.gridLabel4?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
            make.top.equalTo((self.gridImgView4?.snp.bottom)!).offset(12)
        }
    }
    
    @objc func actionToClick(_ sender:UIButton) -> Void {
        if helperBtnClickBlock3 != nil{
            helperBtnClickBlock3!(sender.tag)
        }
       
    }
    
    func setData1(backwaterStr:String,dividendStr:String) -> Void {
        self.backwaterMoneyLabel?.text = backwaterStr
        self.dividendMoneyLabel?.text = dividendStr
        let rkImageURLs = GlobalInfo.gameInfoData?["fanshui_icon"] as? [String:String]
        let bonusImageURLs = GlobalInfo.gameInfoData?["hongli_icon"] as? [String:String]
        if let url = rkImageURLs?["imgNameIOS"] {
            backwaterImgView?.sd_setImage(with: URL(string: url), placeholderImage: imageNamed("img_default_60"), options: .refreshCached, completed: nil)
        }
        if let url = bonusImageURLs?["imgNameIOS"] {
            dividendImgView?.sd_setImage(with: URL(string: url), placeholderImage: imageNamed("img_default_60"), options: .refreshCached, completed: nil)
        }
    }
    
    func setData(dict1:[String:Any]?,dict2:[String:Any]?,dict3:[String:Any]?,dict4:[String:Any]?,row:Int) -> Void {
        //        self.gridBtn1?.isHidden = true
        //        self.gridBtn2?.isHidden = true
        //        self.gridBtn3?.isHidden = true
        //        self.gridBtn4?.isHidden = true
        self.gridBtn1?.tag = 1001
        self.gridBtn2?.tag = 1002
        self.gridBtn3?.tag = 1003
        self.gridBtn4?.tag = 1004
        if dict1 != nil {
            self.gridBtn1?.isHidden = false
            self.gridImgView1?.sd_setImage(with:URL(string:(dict1?["imgurl"] as? String) ?? "")!, placeholderImage: imageNamed("img_default_60"), options: .refreshCached, completed: nil)
            gridLabel1?.text = (dict1?["title"] as? String) ?? ""
        }
        
        if dict2 != nil {
            self.gridBtn2?.isHidden = false
            self.gridImgView2?.sd_setImage(with:URL(string:(dict2?["imgurl"] as? String) ?? "")!, placeholderImage: imageNamed("img_default_60"), options: .refreshCached, completed: nil)
            gridLabel2?.text = (dict2?["title"] as? String) ?? ""
        }
        
        if dict3 != nil {
            self.gridBtn3?.isHidden = false
            self.gridImgView3?.sd_setImage(with:URL(string:(dict3?["imgurl"] as? String) ?? "")!, placeholderImage: imageNamed("img_default_60"), options: .refreshCached, completed: nil)
            gridLabel3?.text = (dict3?["title"] as? String) ?? ""
        }
        
        if dict4 != nil {
            self.gridBtn4?.isHidden = false
            self.gridImgView4?.sd_setImage(with:URL(string:(dict4?["imgurl"] as? String) ?? "")!, placeholderImage: imageNamed("img_default_60"), options: .refreshCached, completed: nil)
            gridLabel4?.text = (dict4?["title"] as? String) ?? ""
        }
    }
    
    @objc func actionToClick3(_ sender:UIButton) -> Void {
        if bonusClickBlock3 != nil{
            bonusClickBlock3!(sender.tag)
        }
    }
}
